package com.smzd.ccms.dao;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.smzd.ccms.domain.Article;
import com.smzd.ccms.mappers.ArticleMapper;
import com.smzd.core.dao.AbstractDao;
import com.smzd.core.mappers.IMapper;
import com.smzd.framework.utils.db.RPage;

@Component
public class ArticleDao extends AbstractDao<Article> {
    @Autowired
    private ArticleMapper mapper;
    
    @Override
    protected IMapper<Article> getMapper() {
        return mapper;
    }

	public Article getFirstArticleByChannelId(int chId) {
		return mapper.getFirstArticleByChannelId(chId);
	}

	public RPage<Article> getArticlesByChId(RPage<Article> page) {
		preparePage(page);
		List<Article> elems = mapper.getArticlesByChId(page);
		page.setElems(elems);
		int totalCount = mapper.getArticlesCountByChId(page);
		page.setTotalCount(totalCount);
		completePage(page);
		return page;
	}

	public RPage<Article> getArticlesByChPath(RPage<Article> page) {
		preparePage(page);
		List<Article> elems = mapper.getArticlesByChPath(page);
		page.setElems(elems);
		int totalCount = mapper.getArticlesCountByChPath(page);
		page.setTotalCount(totalCount);
		completePage(page);
		return page;
	}

	public RPage<Article> getArticlesByPChId(RPage<Article> page) {
		preparePage(page);
		List<Article> elems = mapper.getArticlesByPChId(page);
		page.setElems(elems);
		int totalCount = mapper.getArticlesCountByPChId(page);
		page.setTotalCount(totalCount);
		completePage(page);
		return page;
	}

    public RPage<Article> getArticlesWithLastComment(RPage<Article> page) {
        preparePage(page);
        List<Article> elems = mapper.getArticlesWithLastComment(page);
        page.setElems(elems);
        int totalCount = mapper.getArticlesCount(page);
        page.setTotalCount(totalCount);
        completePage(page);
        return page;
    }

	public Article getUserFirstArticleWithinChannel(int userId, int channelId) {
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		params.put("chId", channelId);
		return mapper.getUserFirstArticleWithinChannel(params);
	}

	public RPage<Article> getUserArticlesWithinChannels(RPage<Article> page) {
		preparePage(page);
		List<Article> elems = mapper.getUserArticlesWithinChannels(page);
		page.setElems(elems);
		int totalCount = mapper.getUserArticlesCountWithinChannels(page);
		page.setTotalCount(totalCount);
		completePage(page);
		return page;
	}

	public void updateArticleStatusAndScope(Article article) {
		mapper.updateArticleStatusAndScope(article);
	}
}